import { useMemo } from "react"
import { useLocation } from "react-router-dom"
import { asyncRoutes } from '@/views'

// 通过useLocation访问当前url，根据url，在route路由信息中遍历找到对应的key及其父级的key
export function useMenu() {
  const { pathname } = useLocation()
  return useMemo(() => {
    let selectedKey = ''
    let openKey = ''
    // 用两层for循环写，更方便地退出循环
    asyncRoutes.forEach(ele => {
      if (!ele.children) {
        if (ele.path === pathname) {
          selectedKey = ele.key
        }
      }
      if (ele.children) {
        ele.children.forEach(item => {
          if (item.path === pathname) {
            selectedKey = item.key
            openKey = ele.key
          }
        })
      }
    })
    // 注意：Menu高亮要求使用 string[] 进行控制
    return [selectedKey + '', openKey + '']
  }, [pathname])
}

// 计算面包屑显示
export function useBreadcrumb() {
  const arr = [...asyncRoutes]
  const { pathname } = useLocation()
  let result = [arr[0]] // 把首页先添加到面包屑中
  // 查找二级菜单
  if (pathname !== '/dashboard') {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].children) {
        for (let j = 0; j < arr[i].children.length; j++) {
          if (arr[i].children[j].path === pathname) {
            result.push(arr[i])
            result.push(arr[i].children[j])
          }
        }
      }
    }
  }
  return result
}

// TagsView
export function useTagsView(paths) {
  let result = [asyncRoutes[0]]
  paths.forEach(item => {
    if (item !== '/dashboard') {
      asyncRoutes.forEach(ele => {
        if (ele.children) {
          ele.children.forEach(val => {
            if (val.path === item) {
              result.push(val)
            }
          })
        } else {
          if (ele.path === item) {
            result.push(ele)
          }
        }
      })
    }
  })
  return result
}